package com.woniuxy.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.commons.exception.StoreException;
import com.woniuxy.commons.exception.code.StoreExceptionCode;
import com.woniuxy.commons.model.Result;
import com.woniuxy.commons.utils.BeanCopyUtil;
import com.woniuxy.commons.utils.DateUtil;
import com.woniuxy.controller.form.GetAllStoreRegisterForm;
import com.woniuxy.controller.form.RejectStoreCheckForm;
import com.woniuxy.controller.form.StoreRegisterForm;
import com.woniuxy.dao.model.StoreInfo;
import com.woniuxy.dao.model.StoreRegister;
import com.woniuxy.service.StoreInfoService;
import com.woniuxy.service.StoreRegisterService;
import com.woniuxy.service.dto.GetMyRegisterStoreDto;
import com.woniuxy.service.dto.PageStoreRegisterDto;
import com.woniuxy.service.dto.UserDetailsDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 商家注册表 前端控制器
 * </p>
 *
 * @author psy
 * @since 2022-06-28
 */
@RestController
@RequestMapping("/storeRegister")
@Api(tags = "商家注册(认证)操作接口")
public class StoreRegisterController {

    @Autowired
    private StoreRegisterService storeRegisterService;

    @Autowired
    private StoreInfoService storeInfoService;

    /**
     * 查询商家自己的认证记录接口,不分页
     */
    @GetMapping("/getMyRegisterStore")
    @ApiOperation("查询商家自己的认证记录接口")
    public Result getMyRegisterStore(){

        List<GetMyRegisterStoreDto> myRegisterStore = storeRegisterService.getMyRegisterStore();

        return Result.success(200,"查询我的认证申请信息成功",myRegisterStore);
    }



    /**
     * 商家注册(认证)的接口
     * @return
     */
    @PostMapping("/registerStore")
    @ApiOperation("商家注册(认证)的接口")
    public Result registerStore(@RequestBody StoreRegisterForm srf){

        if(srf.getPermit() == null || srf.getLicense() == null){
            throw new StoreException(StoreExceptionCode.STORE_CANT_REGISTER);
        }

        if(srf.getStoreName() == null || srf.getLinkman() == null || srf.getAddress() == null || srf.getPhone() == null){
            throw new StoreException(StoreExceptionCode.STORE_CANT_REGISTER_INFO);
        }

        System.out.println(srf.toString());
        if(srf != null){
            List<StoreRegister> listStoreRegister = storeRegisterService.list(new QueryWrapper<StoreRegister>().eq("storeName", srf.getStoreName()));
            if(listStoreRegister != null){
                for(StoreRegister s :listStoreRegister){
                    if(s.getStatus() == 1){
                        throw new StoreException(StoreExceptionCode.STORE_HAD_PASSED);
                    }
                    if(s.getStatus() == 0){
                        throw new StoreException(StoreExceptionCode.STORE_CHECK_ING);
                    }
                }
            }

            Long time = System.currentTimeMillis();
            String times = DateUtil.parseDateLong(time, "yyyy-MM-dd HH:mm:ss");
            StoreRegister storeRegister = BeanCopyUtil.copyOne(srf, StoreRegister.class);
            storeRegister.setStatus(0);
            storeRegister.setCreateUser(srf.getLinkman());
            storeRegister.setCreateDate(times);
            //在商家信息表修改此时已经上传的字段
            UserDetailsDto user = (UserDetailsDto) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            Long id = user.getId();
            StoreInfo storeInfo = storeInfoService.getById(id);
            storeInfo.setStoreName(srf.getStoreName());
            storeInfo.setAddress(srf.getAddress());
            storeInfo.setLicense(srf.getLicense());
            storeInfo.setPermit(srf.getPermit());
            storeInfo.setPhone(srf.getPhone());
            storeInfo.setLinkman(srf.getLinkman());
            storeInfoService.update(storeInfo,new QueryWrapper<StoreInfo>().eq("id",id));
            boolean b = storeRegisterService.save(storeRegister);
        }
        return Result.success(200,"提交商家认证成功，我们会在一个工作日内为您处理",null);
    }


    /**
     * 超级管理员审核商家认证信息时查询所有商家提交的认证申请,分页
     * @return
     */
    @PostMapping("/getAllStoreRegister")
    @ApiOperation("超级管理员审核商家认证信息时查询所有商家提交的认证申请接口")
    public Result getAllStoreRegister(@RequestBody GetAllStoreRegisterForm gasrf){

        PageStoreRegisterDto allStoreRegister = storeRegisterService.getAllStoreRegister(gasrf);

        return Result.success(200,"查询所有商家申请认证信息成功",allStoreRegister);
    }

    /**
     * 超级管理员通过商家认证
     * @param id
     * @return
     */
    @GetMapping("/passCheck")
    @ApiOperation("超级管理员通过商家认证的接口")
    @ApiImplicitParam(name = "id",value = "商家注册表id")
    public Result passCheck(Integer id){
        System.out.println(id);
        UserDetailsDto userDetailsDto = (UserDetailsDto) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        StoreRegister storeRegister = storeRegisterService.getById(id);
        if(storeRegister.getStatus() == 0){
            storeRegister.setStatus(1);
            storeRegister.setUpdateUser(userDetailsDto.getUsername());
            storeRegister.setUpdateDate(DateUtil.parseDateLong(System.currentTimeMillis(),"yyyy-MM-dd HH:mm:ss"));
            storeRegisterService.update(storeRegister,new QueryWrapper<StoreRegister>().eq("id",id));
        }else throw new StoreException(StoreExceptionCode.STORE_CHECK_PASSED_OR_BACK);

        return Result.success(200,"通过认证成功",null);
    }


    /**
     * 超级管理员拒绝商家认证
     * @param rscf
     * @return
     */
    @PostMapping("/rejectCheck")
    @ApiOperation("超级管理员拒绝商家认证的接口")
    public Result rejectCheck(@RequestBody RejectStoreCheckForm rscf){
        UserDetailsDto userDetailsDto = (UserDetailsDto) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        StoreRegister storeRegister = storeRegisterService.getOne(new QueryWrapper<StoreRegister>().eq("id", rscf.getId()));

        if(storeRegister.getStatus() == 0){
            storeRegister.setStatus(2);
            storeRegister.setRejectReason(rscf.getRejectReason());
            storeRegister.setUpdateUser(userDetailsDto.getUsername());
            storeRegister.setUpdateDate(DateUtil.parseDateLong(System.currentTimeMillis(),"yyyy-MM-dd HH:mm:ss"));
            storeRegisterService.update(storeRegister,new QueryWrapper<StoreRegister>().eq("id",rscf.getId()));
        }else throw new StoreException(StoreExceptionCode.STORE_CHECK_PASSED_OR_BACK);

        return Result.success(200,"驳回审批认证成功",null);
    }
}

